package com.clive.llweb.service;

import com.clive.llweb.bean.DishesCategory;
import com.clive.llweb.exception.GlobalException;
import com.clive.llweb.mapper.DishCategoryMapper;
import com.clive.llweb.mapper.DishMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DishCategoryServiceImpl implements DishCategoryService{
    @Autowired
    public DishCategoryMapper dishCategoryMapper;
    @Autowired
    public DishMapper dishMapper;
    @Override
    public List<DishesCategory> findDishesCategoryAll() {
        List<DishesCategory> list = dishCategoryMapper.findDishCategoryAll();
        return list;
    }
//    事务管理 回滚事务  遇到错误将之前删除的sql语句 回滚回来
    @Transactional
//   先删除菜品 再删除菜品分类
//    开启事务
    @Override
    public int deleteDishCategory(Integer id) {
        int i = dishMapper.deleteDishCategoryId(id);
        int j = dishCategoryMapper.deleteDishCategory(id);
        if (j <=0) {
//            在这里判断之后就不用再去controller判断
           throw new GlobalException("删除菜品失败");
        }
//        提交事务
        return j;
    }

}
